package com.amoby.main.service.impl;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.amoby.base.common.Constants;
import com.amoby.base.utils.MD5Encrypt;
import com.amoby.main.entity.SysUsers;
import com.amoby.main.mapper.SysUsersMapper;
import com.amoby.main.service.UserService;

@Service
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = java.lang.Exception.class)
public class UserServiceImpl implements UserService {

    @Autowired
    private SysUsersMapper        sysUsersMapper;

    public int addUser(SysUsers user, String roleId) {
        // 设置默认密码
        user.setPassword(MD5Encrypt.MD5(Constants.DEFAULT_PASSWORD));
        user.setCreateDate(new Date());
        return sysUsersMapper.insert(user);
    }



    public SysUsers getByName(String username) {
        return sysUsersMapper.selectByUsername(username);
    }

    public SysUsers getByUserId(Integer userId) {
        return sysUsersMapper.selectByPrimaryKey(userId);
    }


    @Override
    public boolean resetPassword(Integer userId) {
        SysUsers user = new SysUsers();
        user.setUserId(userId);
        user.setPassword(MD5Encrypt.MD5(Constants.DEFAULT_PASSWORD));
        sysUsersMapper.updateByPrimaryKeySelective(user);
        return true;
    }

    
}
